package ${package}.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package}.mapper.${className}Mapper;
import ${package}.pojo.bean.${className};
import ${package}.pojo.query.${className}Query;
import ${package}.service.${className}Service;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.exception.BadRequestException;
import com.wteam.dragon.system.util.ValidationUtil;
import com.wteam.dragon.system.util.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.io.IOException;
import java.util.ArrayList;

/**
* @Author ${author}
* @Date ${date}
*/
@Service
@RequiredArgsConstructor
public class ${className}ServiceImpl implements ${className}Service {
    private final ${className}Mapper ${changeClassName}Mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage add${className}(${className} ${changeClassName}) {
        ${changeClassName}Mapper.insert(${changeClassName});
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage del${className}(List<Long> ids) {
        ${changeClassName}Mapper.deleteBatchIds(ids);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage edit${className}(${className} ${changeClassName}) {
        ${changeClassName}Mapper.updateById(${changeClassName});
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("更改成功");
        return resultMessage;
    }

    @Override
    public ResultMessage page${className}(Page<${className}> page, ${className}Query ${changeClassName}Query){
        QueryWrapper<${className}> queryWrapper = new QueryWrapper<>();
        <#if queryColumns?? && (queryColumns?size>0)>
        queryWrapper.lambda()
        <#list queryColumns as column>
        <#if column.queryType = 'EQ'>
            <#if column_has_next>
                .eq(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .eq(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'NE'>
            <#if column_has_next>
                .ne(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .ne(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'LIKE'>
            <#if column_has_next>
                .like(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .like(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'NOTLIKE'>
            <#if column_has_next>
                .notLike(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .notLike(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'GT'>
            <#if column_has_next>
                .gt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .gt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'GE'>
            <#if column_has_next>
                .ge(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .ge(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'LT'>
            <#if column_has_next>
                .lt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .lt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'LE'>
            <#if column_has_next>
                .le(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .le(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'ISNOTNULL'>
            <#if column_has_next>
                .isNotNull(${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .isNotNull(${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        </#list>
        </#if>
        <#if orderColumns?? && (orderColumns?size>0)>
        queryWrapper.lambda()
        <#list orderColumns as column>
        <#if column.orderType = 'ASC'>
            <#if column_has_next>
                .orderByAsc(${className}::get${column.capitalColumnName})
            <#else>
                .orderByAsc(${className}::get${column.capitalColumnName});
            </#if>
        </#if>
        <#if column.orderType = 'DESC'>
            <#if column_has_next>
                .orderByDesc(${className}::get${column.capitalColumnName})
            <#else>
                .orderByDesc(${className}::get${column.capitalColumnName});
            </#if>
        </#if>
        </#list>
        </#if>
        Page<${className}> ${changeClassName}Page = ${changeClassName}Mapper.selectPage(page, queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("${changeClassName}Page", ${changeClassName}Page);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public ResultMessage get${className}(Long id) {
        ${className} ${changeClassName} = ${changeClassName}Mapper.selectById(id);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("${changeClassName}", ${changeClassName});
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    public void export${className}(HttpServletResponse response, ${className}Query ${changeClassName}Query) {
        QueryWrapper<${className}> queryWrapper = new QueryWrapper<>();
        <#if queryColumns?? && (queryColumns?size>0)>
        queryWrapper.lambda()
        <#list queryColumns as column>
        <#if column.queryType = 'EQ'>
            <#if column_has_next>
                .eq(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .eq(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'NE'>
            <#if column_has_next>
                .ne(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .ne(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'LIKE'>
            <#if column_has_next>
                .like(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .like(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'NOTLIKE'>
            <#if column_has_next>
                .notLike(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .notLike(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'GT'>
            <#if column_has_next>
                .gt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .gt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'GE'>
            <#if column_has_next>
                .ge(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .ge(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'LT'>
            <#if column_has_next>
                .lt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .lt(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'LE'>
            <#if column_has_next>
                .le(${changeClassName}Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .le(${changeClassName   }Query.get${column.capitalColumnName}() != null, ${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        <#if column.queryType = 'ISNOTNULL'>
            <#if column_has_next>
                .isNotNull(${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}())
            <#else>
                .isNotNull(${className}::get${column.capitalColumnName}, ${changeClassName}Query.get${column.capitalColumnName}());
            </#if>
        </#if>
        </#list>
        </#if>
        <#if orderColumns?? && (orderColumns?size>0)>
        queryWrapper.lambda()
        <#list orderColumns as column>
        <#if column.orderType = 'ASC'>
            <#if column_has_next>
                .orderByAsc(${className}::get${column.capitalColumnName})
            <#else>
                .orderByAsc(${className}::get${column.capitalColumnName});
            </#if>
        </#if>
        <#if column.orderType = 'DESC'>
            <#if column_has_next>
                .orderByDesc(${className}::get${column.capitalColumnName})
            <#else>
                .orderByDesc(${className}::get${column.capitalColumnName});
            </#if>
        </#if>
        </#list>
        </#if>
        List<${className}> ${changeClassName}List = ${changeClassName}Mapper.selectList(queryWrapper);
        List<Map<String, Object>> list = new ArrayList<>();
        ${changeClassName}List.forEach(${changeClassName} -> {
            Dict dict = Dict.create()
            <#list columns as column>
                <#if column.columnKey != 'PRI'>
                <#if column_has_next>
                    .set("${column.remark}", ${changeClassName}.get${column.capitalColumnName}())
                <#else>
                    .set("${column.remark}", ${changeClassName}.get${column.capitalColumnName}());
                </#if>
                </#if>
            </#list>
            list.add(dict);
        });
        try {
            FileUtil.downloadExcel(list, response);
        } catch (IOException e) {
            throw new BadRequestException("IO_ERROR,请联系管理员解决");
        }
    }
}